/**
 * 
 * 新增时间：2010-07-05
 * 新增人：朱家进
 * 新增内容：提交上传文件内容，生成组织和用户(移动自有渠道)
 */

package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.InitParam;
import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParamUtil;
import com.zjht.gmcc.bean.util.MySession;

public class OrgaBatchUploadSubmitGMCC extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {

		ArrayList trueLineList = (ArrayList) session.getAttribute("trueLineList");
		Iterator it = trueLineList.iterator();
		String[] line = null;
		String organame = null;
		String orgacode = null;
		String mobileorgacode = null;
		String countryorgacode = null;
		String halltime = null;
		String halltype = null;
		String hour = null;
		String money = null;
		String jmpassword = MyConstant.INIT_PWD;
		String p2G = null;
		String pTD = null;
		String cityprovidertype = null;
		int flag = 0;

		String areaid = String.valueOf(MySession.getAreaid(session));
		String paraid = null;
		String opercode = MySession.getUsercode(session);
		String sql = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		String paramSql = "SELECT paraid FROM parameter WHERE paraname = ?";
		PreparedStatement paramPs = myJDBC.prepareStatement(paramSql);

		String hallSql = "INSERT INTO organiza(orgacode,organame,parentorgacode,orgatypeid,"
				+ "actorcode,areaid,createdate,shortname,paraid,channeltype,sdate,edate,fillhour,status,orgid,parentorgid) "
				+ "VALUES(?,?,?,?,?,?,sysdate,?,?,?,?,?,?,?,?,?)";
		PreparedStatement hallPs = myJDBC.prepareStatement(hallSql);
		String providerSql = "INSERT INTO mobiletoprovider(mobileorgacode,providerorgacode,cityproviderorgacode,status,validdate,cityprovidertype)"
				+ "VALUES(?,?,?,?,sysdate,?)";
		PreparedStatement providerPs = myJDBC.prepareStatement(providerSql);

		String bossSql = "INSERT INTO mobileorgatolocal(mobileorgacode,orgacode)VALUES(?,?)";
		PreparedStatement bossPs = myJDBC.prepareStatement(bossSql);

		String priceSql = "INSERT INTO SS_PRICE (ID,OrgaCode,Price,CreateDate,CreateOperCode) VALUES (SS_PriceID.NEXTVAL,?,?,SYSDATE,?)";
		PreparedStatement pricePs = myJDBC.prepareStatement(priceSql);

		String opersDutySql = "INSERT INTO OPERSPOPEDOM (opercode,dutyid,operid) VALUES (?,?,?)";
		PreparedStatement operDutyPs = myJDBC.prepareStatement(opersDutySql);

		String opersSql = "INSERT INTO opers(opercode, opername, passwd, orgacode, deptid,createdate, status, opertype, showflag,orgid,operid) "
				+ "VALUES(?,?,?,?,?,sysdate,1,1,1,?,?)";
		PreparedStatement operPs = myJDBC.prepareStatement(opersSql);

		String p2GTDSql = "SELECT a.orgacode provorgacode,b.orgacode cityorgacode  " + "FROM organiza a ,organiza b "
				+ "WHERE b.parentorgacode = a.orgacode AND b.orgatypeid = 6 AND a.orgatypeid = 5  "
				+ "AND b.areaid = ? AND a.shortname = ? AND a.status = 1 AND b.status = 1 AND a.channeltype = '01' " + "AND b.channeltype = '01'";
		PreparedStatement p2GTDPs = myJDBC.prepareStatement(p2GTDSql);
		ResultSet p2GTDRs = null;

		String hallOrgacode = null;// 厅编码
		String hallDZ = null;// 厅店长
		String hallCG = null;// 厅仓管
		String hallOrgid = null;
		String countryOrgid = null;
		String operid = null;
		ResultSet paramRs = null;
		while (it.hasNext()) {
			line = it.next().toString().split("\\|");
			organame = line[0];
			orgacode = line[1].toUpperCase();
			mobileorgacode = line[2].toUpperCase();
			countryorgacode = line[3].toUpperCase();
			halltime = line[4];
			halltype = line[5];
			hour = line[6];
			money = line[7];
			p2G = line[8].trim();
			pTD = line[9].trim();

			hallOrgacode = countryorgacode + orgacode;

			if (pTD.equals("无")) {// 没有选择TD手机供货商
				cityprovidertype = "2";
				p2GTDPs.setString(1, areaid);
				p2GTDPs.setString(2, p2G);
				p2GTDRs = p2GTDPs.executeQuery();
				if (p2GTDRs.next()) {
					providerPs.setString(1, hallOrgacode);
					providerPs.setString(2, p2GTDRs.getString("provorgacode"));
					providerPs.setString(3, p2GTDRs.getString("cityorgacode"));
					providerPs.setString(4, "1");
					providerPs.setString(5, cityprovidertype);
					flag = providerPs.executeUpdate();
				}
			} else if (p2G.equals(pTD)) {// 2G手机和TD手机是同一个供货商

				cityprovidertype = "1";
				p2GTDPs.setString(1, areaid);
				p2GTDPs.setString(2, p2G);
				p2GTDRs = p2GTDPs.executeQuery();
				if (p2GTDRs.next()) {
					providerPs.setString(1, hallOrgacode);
					providerPs.setString(2, p2GTDRs.getString("provorgacode"));
					providerPs.setString(3, p2GTDRs.getString("cityorgacode"));
					providerPs.setString(4, "1");
					providerPs.setString(5, cityprovidertype);
					flag = providerPs.executeUpdate();
				}
			} else {// 2G手机和TD手机不是同一个供货商

				p2GTDPs.setString(1, areaid);
				p2GTDPs.setString(2, p2G);
				p2GTDRs = p2GTDPs.executeQuery();
				if (p2GTDRs.next()) {
					providerPs.setString(1, hallOrgacode);
					providerPs.setString(2, p2GTDRs.getString("provorgacode"));
					providerPs.setString(3, p2GTDRs.getString("cityorgacode"));
					providerPs.setString(4, "1");
					providerPs.setString(5, "2");
					flag = providerPs.executeUpdate();
				}
				if (flag == 1) {
					p2GTDPs.setString(1, areaid);
					p2GTDPs.setString(2, pTD);
					p2GTDRs = p2GTDPs.executeQuery();
					if (p2GTDRs.next()) {
						providerPs.setString(1, hallOrgacode);
						providerPs.setString(2, p2GTDRs.getString("provorgacode"));
						providerPs.setString(3, p2GTDRs.getString("cityorgacode"));
						providerPs.setString(4, "1");
						providerPs.setString(5, "3");
						flag = providerPs.executeUpdate();
					}
				}
			}

			paramPs.setString(1, halltype);
			paramRs = paramPs.executeQuery();
			if (paramRs.next()) {
				paraid = paramRs.getString("paraid");
			}

			hallDZ = countryorgacode + orgacode + "1";// 厅店长
			hallCG = countryorgacode + orgacode + "2";// 厅仓管

			hallOrgid = String.valueOf(MyParamUtil.getSEQ("seq_orgid"));
			sql = "SELECT orgid FROM organiza WHERE orgacode = ? ";
			ps = myJDBC.prepareStatement(sql);
			ps.setString(1, countryorgacode);
			rs = ps.executeQuery();
			countryOrgid = null;
			if (rs.next()) {
				countryOrgid = rs.getString(1);
			}
			rs.close();

			if (flag == 1) {
				hallPs.setString(1, hallOrgacode);
				hallPs.setString(2, organame);
				hallPs.setString(3, countryorgacode);
				hallPs.setString(4, "4");
				hallPs.setString(5, "M");
				hallPs.setString(6, areaid);
				hallPs.setString(7, organame);
				hallPs.setString(8, paraid);
				hallPs.setString(9, MyConstant.CHANNEL_GMCC);
				hallPs.setString(10, hour.split("-")[0]);
				hallPs.setString(11, hour.split("-")[1]);
				hallPs.setString(12, halltime);
				hallPs.setString(13, "1");
				hallPs.setString(14, hallOrgid);
				hallPs.setString(15, countryOrgid);
				flag = hallPs.executeUpdate();
			}

			if (flag == 1) {
				bossPs.setString(1, mobileorgacode);
				bossPs.setString(2, hallOrgacode);
				flag = bossPs.executeUpdate();
			}
			if (flag == 1) {
				pricePs.setString(1, hallOrgacode);
				pricePs.setString(2, money);
				pricePs.setString(3, opercode);
				flag = pricePs.executeUpdate();
			}
			operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
			if (flag == 1) {
				operPs.setString(1, hallDZ);
				operPs.setString(2, "店长");
				operPs.setString(3, jmpassword);
				operPs.setString(4, hallOrgacode);
				operPs.setString(5, "40");
				operPs.setString(6, hallOrgid);
				operPs.setString(7, operid);
				flag = operPs.executeUpdate();
			}
			if (flag == 1) {
				operDutyPs.setString(1, hallDZ);
				operDutyPs.setString(2, "4000");
				operDutyPs.setString(3, operid);
				flag = operDutyPs.executeUpdate();
			}
			operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
			if (flag == 1) {
				operPs.setString(1, hallCG);
				operPs.setString(2, "厅仓管");
				operPs.setString(3, jmpassword);
				operPs.setString(4, hallOrgacode);
				operPs.setString(5, "42");
				operPs.setString(6, hallOrgid);
				operPs.setString(7, operid);
				flag = operPs.executeUpdate();
			}
			if (flag == 1) {
				operDutyPs.setString(1, hallCG);
				operDutyPs.setString(2, "4200");
				operDutyPs.setString(3, operid);
				flag = operDutyPs.executeUpdate();
			}
			if (flag == 1)// 当新增服务厅时，如本市有a.multiplat = '1'平台，则把对应关系加到mobiletoprovider
			{
				sql = "SELECT a.orgacode,a.parentorgacode FROM organiza a "
						+ "WHERE a.multiplat = '1' AND a.areaid = ? AND a.orgatypeid = 6 AND a.actorcode = 'P' AND a.status = '1'";
				ps = myJDBC.prepareStatement(sql);
				ps.setString(1, areaid);
				rs = ps.executeQuery();

				sql = "INSERT INTO mobiletoprovider(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
						+ "ValidDate, CityProviderType,multiplat) " + "VALUES(?, ?, ?, 1, Sysdate, '1','1')";
				ps = myJDBC.prepareStatement(sql);

				while (rs.next()) {
					ps.setString(1, hallOrgacode);
					ps.setString(2, rs.getString("parentorgacode"));
					ps.setString(3, rs.getString("orgacode"));
					flag = ps.executeUpdate();
					if (flag == 0)
						break;
				}
				ps.close();
				ps = null;
			}
			if (flag == 0)
				break;
		}

		if (flag == 1) {
			InitParam.initOrgaInfo();
			alert(out, "移动自有渠道批量导入成功", "/sys/orga/orgabatch_gmcc.jsp");
		} else {

			errAlert(request, out, menu, "移动自有渠道批量导入失败", "/sys/orga/orgabatch_gmcc_view.jsp");
		}

		return flag == 1;

	}
}
